/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c) 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
 *  
 * @original author: jin Chen 
 * @author: jin Chen 
 * @date: Nov 13, 2008$
 * @version: 1.0
 */
package edu.psu.geovista.app.visualclassifier.classify.v2.gui;

import epoviz.trex.common.swing.layout.EvenRowSectionsPane;
import epoviz.trex.common.text.JTextUtils;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Iterator;

public class AskClassificationBoundPane extends JPanel {
    protected JTextField boundFd; //  
    protected ArrayList<JTextField> boudFds; // 

    public AskClassificationBoundPane(double[] bounds) {
        EvenRowSectionsPane p=new EvenRowSectionsPane(bounds.length ,1);
        p.clearControlPane();
        boudFds = new ArrayList<JTextField>();
        for (int i=0;i<bounds.length;i++){
            JTextField fd=new JTextField(String.valueOf(bounds[i]));
            boudFds.add(fd);
            p.addToMainPane(fd);
        }
        JPanel strBDPane=new JPanel();
        JLabel lb=new JLabel("Set boundaries as a string");
        lb.setToolTipText("Set boundaries as a string values delimited by ,. If this is set, the values in the fields below will be ignored");
        boundFd = new JTextField(30);
        strBDPane.add(lb);
        strBDPane.add(boundFd);

       // JPanel mainp=new JPanel();
        this.setLayout(new BorderLayout());
        this.add(strBDPane, BorderLayout.NORTH);
        this.add(p);

    }

    public JTextField getBoundFd() {
        return boundFd;
    }

    public void setBoundFd(JTextField boundFd) {
        this.boundFd = boundFd;
    }

    public ArrayList<JTextField> getBoudFds() {
        return boudFds;
    }

    public void setBoudFds(ArrayList<JTextField> boudFds) {
        this.boudFds = boudFds;
    }
    public double[] getBound(){
         double[]  mybd = null;
        String  strbd = this.getBoundFd().getText().trim();
            if(strbd.length()>0){
                java.util.List<String> bdlist = JTextUtils.getTokenList(strbd, ",");
                mybd=new double[bdlist.size()];
                int i=0;
                for (Iterator stringIterator = bdlist.iterator(); stringIterator.hasNext();) {
                    String sv = (String) stringIterator.next();
                    mybd[i++]=Double.parseDouble(sv);
                }

            }
            else{
                ArrayList<JTextField> fds = this.getBoudFds();
                mybd=new double[fds.size()];
                for (int i=0;i<fds.size() ;i++){
                    JTextField fd = fds.get(i);
                    String sv = fd.getText();
                    mybd[i]=Double.parseDouble(sv);
                }
            }
        return mybd;
    }
}
